package edu.tsinghua.mrpvc.device.mic;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.MicDevice;
import edu.tsinghua.mrpvc.device.simulate.MicSimulateTopic;


public class MicDeviceImpl implements MicDevice, EventHandler {

	//private BundleContext context;
	private String ID;
	private ServiceTracker tracker;
	public MicDeviceImpl(BundleContext context, String ID){
		//this.context = context;
		this.ID = ID;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();
	}
	
	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return new Integer(1).toString();
	}
	
	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return true;
	}
	
	private void generEvent(){
		EventAdmin eventAdmin = (EventAdmin)tracker.getService();
		if(eventAdmin != null){
			Map<String, String> property = new Hashtable<String, String>();
			property.put(MicDevice.EVENT_PROPER_ID, ID);
			property.put(MicDevice.EVENT_PROPER_ISVALID, MicDevice.Constants.ISVALID_TRUE);
			Event stateEvent = new Event(MicDevice.EVENT_TOPIC_STATE, property);
			eventAdmin.postEvent(stateEvent);
		}	
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		if(event.getTopic().equals(MicDevice.EVENT_TOPIC_ISVALID)){
			if(event.getProperty(MicDevice.EVENT_PROPER_ID) != null && event.getProperty(MicDevice.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
			else{
				System.out.println("***MicDevice ID not match***");
			}
		}	
		if(event.getTopic().equals(MicSimulateTopic.MIC_OPEN)){
			openEvent();
		}
		else if(event.getTopic().equals(MicSimulateTopic.MIC_CLOSE)){
			closeEvent();
		}
		
	}
	public void openEvent(){
		EventAdmin eventAdmin = (EventAdmin)tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
			" Mic: recognise owener's voice and post event");
			Map<String, String> property = new Hashtable<String, String>();
			property.put(MicDevice.EVENT_PROPER_ID, ID);
			property.put(MicDevice.EVENT_PROPER_ACTION, MicDevice.Constants.ACTION_OPEN_CURTAIN);
			property.put(MicDevice.EVENT_PROPER_ISVALID, MicDevice.Constants.ISVALID_TRUE);
			Event stateEvent = new Event(MicDevice.EVENT_TOPIC_STATE, property);
			eventAdmin.postEvent(stateEvent);
		}		
	}
	public void closeEvent(){
		EventAdmin eventAdmin = (EventAdmin)tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
			" Mic: recognise owener's voice and post event");
			Map<String, String> property = new Hashtable<String, String>();
			property.put(MicDevice.EVENT_PROPER_ID, ID);
			property.put(MicDevice.EVENT_PROPER_ACTION, MicDevice.Constants.ACTION_CLOSE_CURTAIN);
			Event stateEvent = new Event(MicDevice.EVENT_TOPIC_STATE, property);
			property.put(MicDevice.EVENT_PROPER_ISVALID, MicDevice.Constants.ISVALID_TRUE);
			eventAdmin.postEvent(stateEvent);
		}			
	}
}
